package com.mk.property.controller;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mk.property.common.R;
import com.mk.property.pojo.Charge;
import com.mk.property.service.ChargeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/charge")
public class ChargeController {
    @Autowired
    ChargeService chargeService;

    /**
     * 分页查询
     *
     * @param page     当前查询页码
     * @param pageSize 每页展示的记录数
     * @param roomNumber     房号 - 可选参数
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String roomNumber) {
        log.info("page = {},pageSize = {},roomNumber = {}", page, pageSize, roomNumber);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        //条件构造器
        LambdaQueryWrapper<Charge> lqw = new LambdaQueryWrapper<>();
        //name不为空才会执行下面的语句
        lqw.like(StringUtils.isNotEmpty(roomNumber), Charge::getRoomNumber, roomNumber);
        lqw.orderByDesc(Charge::getChargeDate);
        //执行查询
        chargeService.page(pageInfo, lqw);
        return R.success(pageInfo);
    }
}
